<?php

namespace App\Http\Repositories\EdgeManage;

use App\Http\Repositories\Repository;
use App\Models\Type;

/**
 * 类型相关
 */
class TypeRepo extends Repository
{
    const MODEL = Type::class;

    public function search(array $params)
    {

        $query = $this->query();

        if (isset($params['pid'])) {
            $query->where('pid', $params['pid']);
        }

        if (!empty($params['type_level'])) {
            $query->where('type_level', $params['type_level']);
        }

        if (!empty($params['name'])) {
            $query->where('name', 'like', '%' . $params['name'] . '%');
        }

        if (!empty($params['alias'])) {
            $query->where('alias', 'like', '%' . $params['alias'] . '%');
        }

        if (!empty($params['begin_time'])) {
            $query->where('created_at', '>=', $params['begin_time']);
        }

        if (!empty($params['end_time'])) {
            $query->where('created_at', '<=', $params['end_time']);
        }

        if (!empty($params['order'])) {
            foreach ($params['order'] as $field => $value) {
                $query->orderBy($field, $value);
            }
        } else {
            $query->orderBy('id', 'desc');
        }
        if (!empty($params['page_num']) && empty($params['paginate'])) {
            $query->limit($params['page_num']);
        }

        return $this->paginate($query, $params['paginate'] ?? true, $params['page_num'] ?? 10);
    }
}
